package im.vinci.server.syncdb.persistence;

import im.vinci.server.syncdb.domain.UserFavoriteMusic;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

/**
 * Created by mingjie on 16/10/18.
 */
@Repository
public interface UserFavoriteMusicOperator {

    @Insert({"<script>",
            "insert ignore into user_favorite_music",
            "(user_id, song_id, song_type, share_data, last_favorite_time) values ",
            "<foreach item='item' index='index' collection='list' separator=','>",
            "(#{item.userId}, #{item.songId}, #{item.songType}, #{item.shareData}, #{item.lastFavoriteTime})",
            "</foreach>",
            "</script>"
    })
    @Options(useGeneratedKeys = true, keyColumn = "favorite_music_id")
    int insertData(@Param("list") Collection<UserFavoriteMusic> list);

    @Delete("delete from user_favorite_music where user_id=#{item.userId} and song_id=#{item.songId} and song_type=#{item.songType}")
    int deleteData(@Param("item") UserFavoriteMusic userFavoriteMusic);

    @Select({"<script>",
            "select * from user_favorite_music where user_id=#{user_id} ",
            "<if test='last_favorite_music_id > 0'>",
            "and favorite_music_id &lt; #{last_favorite_music_id} ",
            "</if>",
            "order by favorite_music_id desc limit #{page_size}",
            "</script>"
    })
    List<UserFavoriteMusic> getUserData(@Param("user_id") long userId, @Param("last_favorite_music_id") long lastFavoriteMusicId, @Param("page_size") int pageSize);

}
